#include "MyString.h"

#include <cstring>
MyString::MyString():size(0),capacity(15)
{
    s=new char[capacity];
}

MyString::MyString(const char *str)
{
    size = std::strlen(str);
    capacity=size +1 >15?size+1:15;
    s= new char[capacity];
    std::strcpy(s,str);
}

std::ostream& operator<<(std::ostream &os,const MyString& c)    
    {
        os <<c.s;
        return os;
    }
// std::istream &operator>>(std::istream &is, MyString &string)
//     {
//         char b = 0;
//         while ((b=getchar()) != '\n'&& b != ' ')
//         {
//             string += b;
//         }
//         return is;
//     }

 
MyString::MyString(const MyString &string)
    {
        std::cout << "拷贝构造\n";
        size = string.size;
        capacity = string.capacity;
        s = new char[capacity];
        strcpy(s, string.s);  
    }

MyString&  MyString::operator=(const MyString &string)
{
    delete []s;
    size = string.size;
    capacity = string.capacity;
    s=new char[capacity];
    strcpy(s,string.s);
    return *this;
}




MyString::~MyString()
{
    delete []s;
}



MyString &MyString::operator+=(const MyString &string)
{
    size += string.size;
    if(size + 1>capacity)    
        ReSize(size+1);    
    strcat(this->s,string.s);
    return *this;
}


MyString &MyString::operator+=(const MyString &c)
{
    size += string.size;
    if(size + 1>capacity)    
        ReSize(size+1);    
    strcat(this->s,string.s);
    return *this;
}

MyString MyString::operator+(const MyString & string)
{
    MyString result = *this;
    result += string;
    return result;
}

MyString MyString::operator-(const MyString &string)
{
    MyString result = *this;
    result -= string;
    return result;
}

char &MyString::operator[](int index)
{
    return this -> s[index];
}

bool MyString::operator==(const MyString &string)
{
    return strcmp(s,string.s)==0;
}

bool MyString::operator!=(const MyString &string)
{
   return strcmp(s,string.s)!=0;
}

MyString::operator int()
{
    return atoi(this->s);
}

MyString::operator double()
{
    return atof(this->s);
}


MyString &MyString::operator-=(const MyString &string)
{
    char *dest = strstr(this->s,string.s);
    while(dest != nullptr)
    {
        char *back = dest + string.size;
        while(*back != '\0')
        {
            *dest++ = *back++;
        }
        *dest ='\0';
        this->size-= string.size;
        dest = strstr(this->s,string.s);
    }
    if(size+1 < capacity / 2)
        ReSize(capacity /2);
    return *this;
}

void MyString::ReSize(int capacity)
{
    this->capacity = capacity;
    char *newS = new char[capacity];
    size = size>capacity-1?capacity-1:size;
    strncpy(newS,s,size);
    delete []s;
    s = newS;
}


MyStringArray::MyStringArray(const MyStringArray & a)
{
    size = a.size;
    capacity = a.capacity;
    array = new MyString[capacity];
    for(int i=0;i< size; i++)
    {
        array[i]=a.array[i];
    }
}

MyStringArray &MyStringArray::operator=(const MyStringArray &a)
{
    for(int i=0 ;i< a.size; i++)
    {
        os<<a.array[i]<<std::endl;
    }
    return os;
}

MyStringArray::~MyStringArray()
{
    delete
}

MyStringArray &MyStringArray::operator+=(const MyString &str)
{
    if
}
